// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import TaskManager from "../Tasks/TaskManager";
import PetEntity from "./PetEntity";
import PetTemplate from "./PetTemplate";

export enum PetEvent {
    ADDNEWPET_EVENT = "addNewPet_event",
    PETFIGHT_EVENT = "petFight_event",
    PETTAKEOFF_EVENT = "petTakeoff_event"
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class PetManager implements IModelManager {
    private static _single: PetManager = null;
    private petMap: Map<number, PetTemplate>;
    /**宠物装备格子解锁需求通关关卡配置 */
    petWearUnlockConfig = [20, 60, 100, 140, 180];

    public static get single(): PetManager {
        if (PetManager._single == null) {
            PetManager._single = new PetManager();
        }
        return PetManager._single;
    }

    initConfig(): void {
        this.petMap = new Map();
        var jsonAss = cc.resources.get("configs/pet", cc.JsonAsset);

        for (const iterator of jsonAss.json) {
            var petTp = new PetTemplate();
            petTp.init(iterator);
            this.petMap.set(petTp.id, petTp);
        }
    }

    getPetTemplate(id: number): PetTemplate {
        return this.petMap.get(id);
    }

    getPetTemplateArr(): PetTemplate[] {
        var arr = [];
        this.petMap.forEach((value: PetTemplate, key: number, map: Map<number, PetTemplate>) => {
            arr.push(value);
        }, this);

        return arr;
    }

    getPetEntity(id: number): PetEntity {
        return SaveManager.single.userdata.getPetEntity(id);
    }
    getPetAttribute(attid: number): number {
        var userdata = SaveManager.single.userdata;
        var attributeNum: number = 0;
        for (const iterator of userdata.petWearList) {
            var ey = this.getPetEntity(iterator);
            if (ey != null) {
                for (let i = 0; i < 2; i++) {
                    if (attid == ey.petTp["attributeId" + i]) {
                        attributeNum += ey["attributeValue" + i];
                    }

                }
            }
        }

        return attributeNum;

    }


    isCanFight(): boolean {
        var userdata = SaveManager.single.userdata;
        for (const iterator of userdata.petWearList) {
            if (iterator == 0) {
                return true;
            }
        }
        return false;
    }
    requseUnlockWear() {
        var userdata = SaveManager.single.userdata;

        for (let i = 0; i < this.petWearUnlockConfig.length; i++) {
            for (const iterator of userdata.gatePassList) {
                if (this.petWearUnlockConfig[i] == iterator) {
                    if (userdata.petWearList[i] == -1) {
                        userdata.petWearList[i] = 0;
                        UIManager.single.showTips("宠物格子已解锁。");
                    }
                    break;
                }
            }
        }
        SaveManager.single.saveData();
    }

    requestGetPet(id: number) {
        var petEy = this.getPetEntity(id);
        if (petEy != null) {
            petEy.getExp(petEy.expValue);
        } else {
            petEy = new PetEntity({ id: id, level: 1, exp: 0, isFight: false, evolveLevel: 0 });
            var userdata = SaveManager.single.userdata;
            userdata.petBagList.push(petEy);
            UIManager.single.showTips("获得宠物<@>".replace("@", petEy.petTp.petName));
        }

        SaveManager.single.saveData();
        GameEvent.eventNode.emit(PetEvent.ADDNEWPET_EVENT);

        TaskManager.single.requestUpdataTaskPregress(106, 1);
    }

    requestPetFight(id: number) {
        if (!this.isCanFight()) {
            UIManager.single.showTips("无法出战。");
            return false;
        }

        var petEy = this.getPetEntity(id);
        petEy.isFight = true;

        var userdata = SaveManager.single.userdata;
        for (let i = 0; i < userdata.petWearList.length; i++) {
            if (userdata.petWearList[i] == 0) {
                userdata.petWearList[i] = id;
                break;
            }
        }
        SaveManager.single.saveData();
        UIManager.single.showTips("出战成功。");
        GameEvent.eventNode.emit(PetEvent.PETFIGHT_EVENT);
    }

    requestPetTakeoff(id: number) {
        var petEy = this.getPetEntity(id);
        petEy.isFight = false;

        var userdata = SaveManager.single.userdata;
        for (let i = 0; i < userdata.petWearList.length; i++) {
            if (userdata.petWearList[i] == id) {
                userdata.petWearList[i] = 0;
                break;
            }
        }
        SaveManager.single.saveData();
        UIManager.single.showTips("卸下成功。");
        GameEvent.eventNode.emit(PetEvent.PETTAKEOFF_EVENT);
    }


}
